﻿

using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;

namespace ModelRelationship.OwnedType
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }

        public UserInfo UserInfo { get; set; } = new UserInfo();
    }

    [Owned]
    [Table("UserInfo")]
    public class UserInfo
    {
        public string NickName { get; set; }
        public string PersonalTag { get; set; }
    }

    /// <summary>
    /// 从属实体类型
    /// </summary>
    public class OneToOneRelationshipDb : DbContext
    {
        public DbSet<User> Users => Set<User>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>(
                b =>
                {
                    b.OwnsOne(u => u.UserInfo)
                     .ToTable("UserInfos");
                });
        }

    }
}
